package com.liying.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;

import com.liying.Converter;
import com.liying.Graph;
import com.liying.LinkAlgorithm;

@SuppressWarnings("serial")
public class LiYingServlet extends HttpServlet {

	public LiYingServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String fileName = request.getParameter("dataset");
		String alg_type = request.getParameter("func_type");
		String topK = request.getParameter("topK");
				
		String baseFilePath = getServletContext().getRealPath("/") + "duansky"
				+ File.separator;
		System.out.println("baseFilePath:" + baseFilePath);

		Converter converter = new Converter();
		Graph graph = converter.jsonToGraph(converter
				.readJsonFromFile(baseFilePath + "data" + File.separator
						+ fileName));
		Graph complementGraph = graph.buildComplementGraph();

		if(alg_type.equals("CN"))
			LinkAlgorithm.computeCommonNeighbors(graph, complementGraph);
		else if(alg_type.equals("JS"))
			LinkAlgorithm.computeJaccardSimilarity(graph, complementGraph);
		else if(alg_type.equals("AA"))
			LinkAlgorithm.computeAdamicAdar(graph, complementGraph);
		else if(alg_type.equals("PA"))
			LinkAlgorithm.computePreferentialAttachment(graph, complementGraph);
		else{
			System.out.println("error");
			return;
		}
		
		JSONArray jsonArr = converter.topResultToJsonArray(
				complementGraph.findTops(Integer.parseInt(topK)), graph.getEdgeNum());
		System.out.println("method:  " + alg_type);
		System.out.println("\n" + jsonArr.toString() + "\n");

		response.setContentType("application/x-json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(jsonArr);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
